<template>
    <el-dialog title="SRM-送货单明细管理—明细" v-model="visible" width="65%"
               :close-on-click-modal="false" draggable>
        <div style="height: 600px">
            <el-row>
                <div class="mb8" style="width: 100%">
                    <el-button icon="printer" plain type="info" class="ml10" @click="chinesePrinting" :disabled="form.chineseButton">
                        {{ $t('srmAsnD.chinesePrinting') }}
                    </el-button>
                    <el-button icon="printer" plain type="info" class="ml10" @click="englishPrinting" :disabled="form.englishButton">
                        {{ $t('srmAsnD.englishPrinting') }}
                    </el-button>
                </div>
            </el-row>
            <el-table :data="state.dataList" v-loading="state.loading" border style="width: 100%"
                      :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
                      @sort-change="sortChangeHandle">
                <el-table-column prop="asnCode" fixed :label="$t('podetail.asnCode')" show-overflow-tooltip  width="200"/>
                <el-table-column prop="asnQty" :label="$t('srmAsnD.qty')" show-overflow-tooltip  width="80"/>
                <el-table-column prop="barcode" :label="$t('srmAsnD.barcode')" show-overflow-tooltip width="180"/>
                <el-table-column prop="barcodeQty" :label="$t('srmAsnD.barcodeQty')" show-overflow-tooltip  width="120"/>
                <el-table-column prop="productDate" :label="$t('srmAsnD.productionDate')" show-overflow-tooltip  width="150"/>
                <el-table-column prop="produceBatch" :label="$t('srmAsnD.produceBatch')" show-overflow-tooltip  width="150"/>
                <el-table-column prop="productEquipment" :label="$t('srmAsnD.productEquipment')" show-overflow-tooltip  width="150"/>
                <el-table-column prop="poCode" :label="$t('poheader.poCode')" show-overflow-tooltip width="150"/>
                <el-table-column prop="contractNumber" :label="$t('poheader.contractNumber')" show-overflow-tooltip width="150"/>
                <el-table-column prop="remark" :label="$t('srmAsnD.remark')" show-overflow-tooltip  width="150"/>
            </el-table>
            <!--        <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>-->
        </div>
        <template #footer>
            <span class="dialog-footer">
                <el-button icon="close" @click="visible = false"> {{ $t('srmAsnD.cancel') }}</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts" name="SrmPoheaderDialog">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {InsertBarcodeBySrmBarcode, selectByAsnDId} from "/@/api/platform/srm/srmAsnH";
import {viewReport} from '/@/hooks/viewReport'
import {ElMessage} from "element-plus";

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)

const state: BasicTableProps = reactive<BasicTableProps>({
    queryForm: {},
    pageList: selectByAsnDId,
    isPage: false
})

//  table hook
const {
    getDataList,
    currentChangeHandle,
    sizeChangeHandle,
    sortChangeHandle,
    tableStyle
} = useTable(state)



// 提交表单数据
const form = reactive({
    asnCode:'',
    asnQty:'',
    id:'',
    asnHId:'',
    materialcode:'',
    englishButton: false,
    chineseButton: false,
});

// 打开弹窗
const openDialog = (id,asnCode,asnQty,asnHId,materialcode) => {
    //查询参数id
    state.queryForm.id = id
    form.id = id
    form.asnCode = asnCode
    form.asnQty = asnQty
    form.asnHId = asnHId
    form.materialcode = materialcode
    getDataList(true)
    visible.value = true

    // 重置表单数据
    nextTick(() => {
        dataFormRef.value?.resetFields();
    });
};

// 中文打印
const chinesePrinting = async () => {
    form.chineseButton = true
    let params = {
        asnDetailId : form.id,
    }
    await InsertBarcodeBySrmBarcode(params).then(res =>{
        if(res.code == 0){
            viewReport("送货单条码", {asnHid: form.asnHId,materialCode:form.materialcode})
            form.chineseButton = false
        }else {
            ElMessage.error('条码插入失败');
        }
    });
};
//英文打印
const englishPrinting = async () => {
    form.englishButton = true
    let params = {
        asnDetailId : form.id,
    }
    await InsertBarcodeBySrmBarcode(params).then(res =>{
        if(res.code == 0){
            viewReport("srm_Delivery_Barcode_en", {asnHid: form.asnHId,materialCode:form.materialcode})
            form.englishButton = false
        }else {
            ElMessage.error('条码插入失败');
        }
    });
};


// 暴露变量
defineExpose({
    openDialog
});
</script>